// (c) Copyright Fernando Cerqueira.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Orientation = System.Windows.Controls.Orientation;

// ReSharper disable CheckNamespace
namespace WindowsPhoneCommands
// ReSharper restore CheckNamespace
{
    /// <summary>
    /// This Class Provides the TreeView with extended functionalities like,
    /// Adding the HeaderText feature to Node, Setting the icon for TreeViewNode.
    /// </summary>
    public class TreeViewWithIcons : TreeViewItem
    {
        private ImageSource _iconSource;
        private readonly TextBlock _textBlock;
        private readonly Image _icon;
        private readonly CheckBox _checkBox;
        private bool _checkedVisibled;

        public static readonly RoutedEvent ActionEvent =
            EventManager.RegisterRoutedEvent("ChangeCheckItem", RoutingStrategy.Bubble,
                                             typeof(RoutedEventHandler), typeof(TreeViewWithIcons));

        public event RoutedEventHandler ChangeCheckItem
        {
            add { AddHandler(ActionEvent, value); }
            remove { RemoveHandler(ActionEvent, value); }
        }


        public TreeViewWithIcons()
        {
            var stack = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(0, 0, 0, 5) };
            Header = stack;
            //Uncomment this code If you want to add an Image after the Node-HeaderText
            //textBlock = new TextBlock();
            //textBlock.VerticalAlignment = VerticalAlignment.Center;
            //stack.Children.Add(textBlock);

            _checkBox = new CheckBox
                            {
                                Focusable = false,
                                IsThreeState = true,
                                IsChecked = false,
                                Visibility = Visibility.Visible,
                                VerticalAlignment = VerticalAlignment.Center,
                                Margin = new Thickness(0, 0, 4, 0),
                            };
            _checkBox.Click += CheckBoxClick;
            stack.Children.Add(_checkBox);
            _icon = new Image
                        {
                            VerticalAlignment = VerticalAlignment.Center,
                            Margin = new Thickness(0, 0, 4, 0),
                            Source = _iconSource
                        };
            stack.Children.Add(_icon);
            //Add the HeaderText After Adding the icon
            _textBlock = new TextBlock { VerticalAlignment = VerticalAlignment.Center };
            stack.Children.Add(_textBlock);

        }

        private void CheckBoxClick(object sender, RoutedEventArgs e)
        {
            Focus();
            var tempact = ActionEvent;
            if (tempact != null)
            {
                var arg = new TreeViewCheckClickEvent(this) { RoutedEvent = tempact };
                RaiseEvent(arg);
            }
        }


        /// <summary>
        /// Gets/Sets the Selected Image for a TreeViewNode
        /// </summary>
        public ImageSource Icon
        {
            set
            {
                _iconSource = value;
                _icon.Source = _iconSource;
            }

            get
            {
                return _iconSource;
            }

        }

        /// <summary>
        /// Gets/Sets the Selected Height Image for a TreeViewNode
        /// </summary>
        public double IconHeight
        {
            set
            {
                _icon.Height = value;
            }

            get
            {
                return _icon.Height;
            }

        }

        /// <summary>
        /// Gets/Sets the Selected width Image for a TreeViewNode
        /// </summary>
        public double IconWidth
        {
            set
            {
                _icon.Width = value;
            }

            get
            {
                return _icon.Width;
            }

        }

        public bool CheckedVisibled
        {
            set
            {
                _checkedVisibled = value;
                var statevisible = _checkedVisibled ? Visibility.Visible : Visibility.Collapsed;
                _checkBox.Visibility = statevisible;
            }
            get
            {
                return _checkedVisibled;
            }
        }

        public bool? Checked
        {
            set
            {
                _checkBox.IsChecked = value;
            }
            get
            {
                return _checkBox.IsChecked;
            }
        }

        /// <summary>
        /// Gets/Sets the HeaderText of TreeViewWithIcons
        /// </summary>
        public string HeaderText
        {
            set
            {
                _textBlock.Text = value;
            }
            get
            {
                return _textBlock.Text;
            }
        }


        public bool IsDevice { get; set; }
        public bool IsRootApp { get; set; }
        public bool IsAppUser { get; set; }
        public bool IsAppCore { get; set; }

    }

}